package com.cddata;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import com.cddata.entity.ContentType;


public class HelperBean {

	int mainWindowHieght = 0;
	int mainWindowWidth = 0;
	int lines = 0;
	int insertedLines = 0;
	boolean processFinished = false;
	Object holdContentObject = null;
	String labelText = null;
	String fileName = null;
	JPanel dataPanel = null;
	public HashMap<String,String> contentTypes = new HashMap<String, String>();
	
	public HashMap<String,String> getContentTypes() {
		return contentTypes;
	}
	
	public String getContentTypes( String aTypeId ) {
		return contentTypes.get(aTypeId);
	}
	
	@SuppressWarnings("unchecked")
	public String getTypeId( String aExtension ) {
		Set keySet = contentTypes.keySet();
		String typeId =  null; 
		for( Iterator it = keySet.iterator(); it.hasNext(); ) {
			typeId = it.next().toString();
			if( aExtension.equals( contentTypes.get(typeId) ) ) {
				return typeId;
			}
		}
		return typeId;
	}
	
	public void setContentTypes( ContentType aCt ) {
		contentTypes.put(new Integer(aCt.getTypeId()).toString(), aCt.getTypeName().toUpperCase());
	}
	
	public void setContentTypes( String aTypeId, String aTypeName ) {
		contentTypes.put(aTypeId, aTypeName);
	}
	

	public JPanel getDataPanel() {
		return dataPanel;
	}

	public void setDataPanel(JPanel dataPanel) {
		this.dataPanel = dataPanel;
	}

	public int getMainWindowHieght() {
		return mainWindowHieght;
	}

	public void setMainWindowHieght(int mainWindowHieght) {
		this.mainWindowHieght = mainWindowHieght;
	}

	public int getMainWindowWidth() {
		return mainWindowWidth;
	}

	public void setMainWindowWidth(int mainWindowWidth) {
		this.mainWindowWidth = mainWindowWidth;
	}
	
	public  boolean isProcessFinished() {
		return processFinished;
	}
	
	public  void setProcessFinished(boolean processFinished) {
		this.processFinished = processFinished;
	}
	
	public  int getLines() {
		return lines;
	}
	
	public  void setLines(int lines) {
		this.lines = lines;
	}
	
	public  int getInsertedLines() {
		return insertedLines;
	}

	public  void setInsertedLines(int insertedLines) {
		this.insertedLines = insertedLines;
	}
	
	public  Object getStoredObject () {
		return holdContentObject;
	}
	
	public  void setStoredObject( Object aObject ) {
		this.holdContentObject = aObject;
	}
	
	public  String getFileName() {
		return fileName;
	}

	public  void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public  String getLabelText() {
		return labelText;
	}

	public  void setLabelText(String labelText) {
		this.labelText = labelText;
	}
	

	/**
	 * hides the content panel which is stored in the the object variable
	 */
	public  void hideContainer( ) {
		if( holdContentObject!= null ) {
			if( holdContentObject instanceof SearchPane) {
				SearchPane search = (SearchPane)holdContentObject;
				search.setVisible(false);
				search = null;
			} else if(holdContentObject instanceof AvailableCDs) {
				AvailableCDs availCds = (AvailableCDs)holdContentObject;
				availCds.setVisible(false);
				availCds= null;
			} else if(holdContentObject instanceof SearchResult) {
				SearchResult searchResult = (SearchResult)holdContentObject;
				searchResult.setVisible(false);
				searchResult= null;
			}else if(holdContentObject instanceof FileUpload) {
				FileUpload upload = (FileUpload)holdContentObject;
				upload.setVisible(false);
				upload = null;
			}else if( holdContentObject instanceof LoadCDData ) {
				LoadCDData load = ( LoadCDData)holdContentObject;
				load.setVisible(false);
				load = null;
			}
		}
	}
	
	public  void addContainer( Object aObject ) {
		this.setStoredObject(aObject);
		if( aObject instanceof Menu) {
			dataPanel.add((Menu)aObject);
		} else if(aObject instanceof AvailableCDs ) {
			AvailableCDs availCd = (AvailableCDs)aObject;
			availCd.setVisible(true);
			dataPanel.add(availCd,BorderLayout.CENTER);
		} else if( aObject instanceof SearchPane ) {
			SearchPane search = (SearchPane)aObject;
			search.setVisible(true);
			dataPanel.add(search);
		}else if( aObject instanceof SearchResult ) {
			SearchResult searchResult = (SearchResult)aObject;
			searchResult.setVisible(true);
			dataPanel.add(searchResult);
		}else if( aObject instanceof FileUpload ) {
			FileUpload upload = (FileUpload)aObject;
			upload.setVisible(true);
			dataPanel.add(upload);
		}else if( aObject instanceof LoadCDData ) {
			LoadCDData load = (LoadCDData)aObject;
			load.setVisible(true);
			dataPanel.add(load);
		}
		dataPanel.repaint();
	}
	
	public  String convertBytesToKBMB ( long aSize ) {
		String size = null;
		if( aSize > 1023 && aSize < 1048576 ) {
			size = new Long( aSize/1024).toString() + " KB";
		} else if( aSize > 1048575 ) {
			size = new Long( aSize/(1024*1024)).toString() + " MB";
		} else {
			size = new Long( aSize).toString() + " B";
		}
		return size ;
	}
	
	public JPanel createHeaderPanel( String aTitle , Dimension aDimension ) {
		JPanel contentHeading = new JPanel();
		contentHeading.add(new JLabel(aTitle));
		contentHeading.setBorder(new TitledBorder(""));
		if( aDimension !=null) {
			contentHeading.setMaximumSize(aDimension);
		}
		return contentHeading;
	}
	
	
	
}
